package com.se.utils;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;

public class DateUtil
{
  public static String getTime()
  {
    return getTime("yyyyMMddHHmmss");
  }

  public static String getDate()
  {
    return getTime("yyyyMMdd");
  }

  public static String getTime(String paramString)
  {
    Calendar localCalendar = Calendar.getInstance();
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString);
    return localSimpleDateFormat.format(localCalendar.getTime());
  }

  public static java.util.Date stringToDate(String paramString1, String paramString2)
  {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString2);
    ParsePosition localParsePosition = new ParsePosition(0);
    return localSimpleDateFormat.parse(paramString1, localParsePosition);
  }

  public static Calendar stringToCalendar(String paramString1, String paramString2)
  {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString2);
    ParsePosition localParsePosition = new ParsePosition(0);
    java.util.Date localDate = localSimpleDateFormat.parse(paramString1, localParsePosition);
    Calendar localCalendar = Calendar.getInstance();
    localCalendar.setTime(localDate);
    return localCalendar;
  }

  public static String formatDate(java.util.Date paramDate, String paramString)
  {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString);
    Calendar localCalendar = Calendar.getInstance();
    localCalendar.setTime(paramDate);
    return localSimpleDateFormat.format(localCalendar.getTime());
  }

  public static String formatCalendar(Calendar paramCalendar, String paramString)
  {
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString);
    return localSimpleDateFormat.format(paramCalendar.getTime());
  }

  public static boolean isLeapYear(int paramInt)
  {
    if (paramInt % 4 != 0)
      return false;
    if (paramInt % 400 == 0)
      return true;
    return paramInt % 100 != 0;
  }

  public static int lastDate(int paramInt1, int paramInt2)
  {
    int[] arrayOfInt = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    if ((paramInt2 > 12) || (paramInt2 < 0))
      paramInt2 = 0;
    if ((paramInt2 == 2) && (isLeapYear(paramInt1)))
      return arrayOfInt[paramInt2] + 1;
    return arrayOfInt[paramInt2];
  }
  /**
   * return add day to date strings
   * @param String date string
   * @param int ???? ???
   * @return String ??￥ ????? ?°?, ??????? ??￥?? ?? ??? ?????
   *           ????? ??? ????? ???????? ??? ??￥: java.text.ParseException ???
   * @exception         Exception
   */
  public static String addDays(String s, int day) throws Exception {
      return addDays(s, day, "yyyyMMdd");
  }

  /**
   * return add day to date strings with user defined format.
   * @param String date string
   * @param String ???? ???
   * @param format string representation of the date format. For example, "yyyy-MM-dd".
   * @return int ??￥ ????? ?°?, ??????? ??￥?? ?? ??? ?????
   *           ????? ??? ????? ???????? ??? ??￥: java.text.ParseException ???
   * @exception         Exception
   */
  public static String addDays(String s, int day, String format)
      throws Exception {
      try {
          java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format,
                  java.util.Locale.KOREA);
          java.util.Date date = check(s, format);

          date.setTime(date.getTime() + ((long) day * 1000 * 60 * 60 * 24));

          return formatter.format(date);
      } catch (Exception e) {
          throw new Exception("[DateUtil][addDays]" + e.getMessage(), e);
      }
  }
  /**
   * check date string validation with the default format "yyyyMMdd".
   * @param s date string you want to check with default format "yyyyMMdd".
   * @return date java.util.Date
   */
   private static java.util.Date check(String s)
       throws java.text.ParseException {
       return check(s, "yyyyMMdd");
   }

   /**
    * check date string validation with an user defined format.
    * @param s date string you want to check.
    * @param format string representation of the date format. For example, "yyyy-MM-dd".
    * @return date java.util.Date
    */
   private static java.util.Date check(String s, String format)
       throws java.text.ParseException {
       if (s == null) {
           throw new java.text.ParseException("date string to check is null", 0);
       }

       if (format == null) {
           throw new java.text.ParseException("format string to check date is null",
               0);
       }

       java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(format,
               java.util.Locale.KOREA);
       java.util.Date date = null;

       try {
           date = formatter.parse(s);
       } catch (java.text.ParseException e) {
           /*
           throw new java.text.ParseException(
                   e.getMessage() + " with format \"" + format + "\"",
                   e.getErrorOffset()
           );
           */
           throw new java.text.ParseException(" wrong date:\"" + s +
               "\" with format \"" + format + "\"", 0);
       }

       if (!formatter.format(date).equals(s)) {
           throw new java.text.ParseException("Out of bound date:\"" + s +
               "\" with format \"" + format + "\"", 0);
       }

       return date;
   }

   
  public static int daysDiff(String paramString1, String paramString2, String paramString3)
  {
    if ((paramString1 == null) || (paramString2 == null))
      return 0;
    java.util.Date localDate1 = stringToDate(paramString1, paramString3);
    java.util.Date localDate2 = stringToDate(paramString2, paramString3);
    return (int)((localDate2.getTime() - localDate1.getTime()) / 86400000L);
  }

  public static int compareDate(String paramString1, String paramString2, String paramString3)
  {
    Calendar localCalendar1 = stringToCalendar(paramString1, paramString3);
    Calendar localCalendar2 = stringToCalendar(paramString2, paramString3);
    return compareDate(localCalendar1, localCalendar2);
  }

  public static int compareDate(java.util.Date paramDate1, java.util.Date paramDate2)
  {
    Calendar localCalendar1 = Calendar.getInstance();
    localCalendar1.setTime(paramDate1);
    Calendar localCalendar2 = Calendar.getInstance();
    localCalendar2.setTime(paramDate2);
    return compareDate(localCalendar1, localCalendar2);
  }

  public static int compareDate(Calendar paramCalendar1, Calendar paramCalendar2)
  {
    int i = 9;
    if (paramCalendar1.before(paramCalendar2))
      i = -1;
    if (paramCalendar1.after(paramCalendar2))
      i = 1;
    if (paramCalendar1.equals(paramCalendar2))
      i = 0;
    return i;
  }

  public static java.sql.Date rollYears(java.util.Date paramDate, int paramInt)
  {
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    localGregorianCalendar.setTime(paramDate);
    localGregorianCalendar.add(1, paramInt);
    return new java.sql.Date(localGregorianCalendar.getTime().getTime());
  }

  public static java.sql.Date rollMonths(java.util.Date paramDate, int paramInt)
  {
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    localGregorianCalendar.setTime(paramDate);
    localGregorianCalendar.add(2, paramInt);
    return new java.sql.Date(localGregorianCalendar.getTime().getTime());
  }

  public static java.sql.Date rollDays(java.util.Date paramDate, int paramInt)
  {
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    localGregorianCalendar.setTime(paramDate);
    localGregorianCalendar.add(5, paramInt);
    return new java.sql.Date(localGregorianCalendar.getTime().getTime());
  }

  public static String getTomorrow()
  {
    return getDate(1, "yyyyMMdd");
  }

  public static String getTomorrow(String paramString)
  {
    return getDate(1, paramString);
  }

  public static String getYesterday()
  {
    return getDate(-1, "yyyyMMdd");
  }

  public static String getYesterday(String paramString)
  {
    return getDate(-1, paramString);
  }

  public static String getDate(int paramInt)
  {
    return getDate(paramInt, "yyyyMMdd");
  }

  public static String getDate(int paramInt, String paramString)
  {
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    SimpleTimeZone localSimpleTimeZone = new SimpleTimeZone(32400000, "Asia/Seoul");
    localGregorianCalendar.setTimeZone(localSimpleTimeZone);
    SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString);
    localGregorianCalendar.add(5, paramInt);
    return localSimpleDateFormat.format(localGregorianCalendar.getTime());
  }

  public static String dateFilter(String paramString1, String paramString2)
  {
    if ((paramString1 == null) || (paramString1.length() < 14) || (paramString2.length() < 2))
      return "";
    String str = "";
    int i = Integer.parseInt(paramString1.substring(0, 4));
    int j = Integer.parseInt(paramString1.substring(2, 4));
    int k = Integer.parseInt(paramString1.substring(4, 6));
    int m = Integer.parseInt(paramString1.substring(6, 8));
    int n = Integer.parseInt(paramString1.substring(8, 10));
    int i1;
    if (n > 12)
      i1 = n - 12;
    else
      i1 = n;
    int i2 = Integer.parseInt(paramString1.substring(10, 12));
    int i3 = Integer.parseInt(paramString1.substring(12, 14));
    str = replaceString(paramString2, "YYYY", intToString(i));
    str = replaceString(str, "YY", intToString(j));
    str = replaceString(str, "MM", intToString(k));
    str = replaceString(str, "DD", intToString(m));
    str = replaceString(str, "HH24", intToString(n));
    str = replaceString(str, "HH", intToString(i1));
    str = replaceString(str, "MI", intToString(i2));
    str = replaceString(str, "SS", intToString(i3));
    return str;
  }

  public static String dateFilter(String paramString1, String paramString2, boolean paramBoolean)
  {
    if ((paramString1 == null) || (paramString1.length() < 14) || (paramString2.length() < 2))
      return "";
    String str1 = "";
    String str2 = "";
    int i1;
    if (paramBoolean)
    {
      int i4 = Integer.parseInt(paramString1.substring(8, 10));
      if (i4 > 12)
        i1 = i4 - 12;
      else
        i1 = i4;
      str2 = intToString(i1);
      if (str2.length() <= 1)
        str2 = "0" + str2;
      str1 = replaceString(paramString2, "YYYY", paramString1.substring(0, 4));
      str1 = replaceString(str1, "YY", paramString1.substring(2, 4));
      str1 = replaceString(str1, "MM", paramString1.substring(4, 6));
      str1 = replaceString(str1, "DD", paramString1.substring(6, 8));
      str1 = replaceString(str1, "HH24", paramString1.substring(8, 10));
      str1 = replaceString(str1, "HH", str2);
      str1 = replaceString(str1, "MI", paramString1.substring(10, 12));
      str1 = replaceString(str1, "SS", paramString1.substring(12, 14));
    }
    else
    {
      int i = Integer.parseInt(paramString1.substring(0, 4));
      int j = Integer.parseInt(paramString1.substring(2, 4));
      int k = Integer.parseInt(paramString1.substring(4, 6));
      int m = Integer.parseInt(paramString1.substring(6, 8));
      int n = Integer.parseInt(paramString1.substring(8, 10));
      if (n > 12)
        i1 = n - 12;
      else
        i1 = n;
      int i2 = Integer.parseInt(paramString1.substring(10, 12));
      int i3 = Integer.parseInt(paramString1.substring(12, 14));
      str1 = replaceString(paramString2, "YYYY", intToString(i));
      str1 = replaceString(str1, "YY", intToString(j));
      str1 = replaceString(str1, "MM", intToString(k));
      str1 = replaceString(str1, "DD", intToString(m));
      str1 = replaceString(str1, "HH24", intToString(n));
      str1 = replaceString(str1, "HH", intToString(i1));
      str1 = replaceString(str1, "MI", intToString(i2));
      str1 = replaceString(str1, "SS", intToString(i3));
    }
    return str1;
  }

  private static String intToString(int paramInt)
  {
    return new Integer(paramInt).toString();
  }

  private static String replaceString(String paramString1, String paramString2, String paramString3)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    int i = -1;
    if ((paramString1 == null) || (paramString2 == null) || (paramString2.equals("")))
      return paramString1;
    if (paramString3 == null)
      paramString3 = "";
    while (true)
    {
      if ((i = paramString1.indexOf(paramString2)) == -1)
      {
        localStringBuffer.append(paramString1);
        break;
      }
      localStringBuffer.append(paramString1.substring(0, i)).append(paramString3);
      paramString1 = paramString1.substring(i + paramString2.length());
    }
    return localStringBuffer.toString();
  }

  public static long getTimeInMillis()
  {
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    SimpleTimeZone localSimpleTimeZone = new SimpleTimeZone(32400000, "Asia/Seoul");
    localGregorianCalendar.setTimeZone(localSimpleTimeZone);
    return localGregorianCalendar.getTimeInMillis();
  }

  public static java.util.Date getDateObject()
  {
    GregorianCalendar localGregorianCalendar = new GregorianCalendar();
    SimpleTimeZone localSimpleTimeZone = new SimpleTimeZone(32400000, "Asia/Seoul");
    localGregorianCalendar.setTimeZone(localSimpleTimeZone);
    return localGregorianCalendar.getTime();
  }

  public static boolean isBeetweenDate(java.util.Date paramDate1, java.util.Date paramDate2, java.util.Date paramDate3)
  {
    boolean i = false;
    if ((compareDate(paramDate1, paramDate3) == -1) && (compareDate(paramDate2, paramDate3) == 1))
      i = true;
    return i;
  }
}